package api;

import cn.hutool.json.JSONUtil;
import model.UserInfo;
import util.AppVar;
import util.ResultUtils;
import util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: wenjingyuan
 * @Date: 2023/03/22/17:00
 * @Description:得到用户登录详情的servlet
 */
@WebServlet("/loginUserInfo")
public class LoginUserInfo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserInfo userInfo= SessionUtils.getLogInUser(req);
        String msg="未知错误!";

        //2.从session对象中获取当前登陆的用户
        if(userInfo!=null){
            //当前用户已登录
            userInfo.setPassword("");//去掉返回信息中的敏感数据
            msg="登陆成功！";
        }else {
            msg="未登录";
        }
        //3.将用户返回给前端
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().println(JSONUtil.toJsonStr(ResultUtils.toResult(200,userInfo,msg)));
    }
}
